import {
    CodeLensProvider,
    TextDocument,
    CodeLens,
    Range
} from "vscode";
import * as fse from "fs-extra"
import { SUBMIT_SINGLE_LANGUAGE_CODING } from "./contstants/Commands";
import { twsManager } from "./TwsManager";
class SingleLanguageCodeingLensProvider implements CodeLensProvider {
    private codeLens: any | undefined;
    private assignment: any = {};

    init(assignment: any) {
        this.assignment = assignment
        return this
    }
    needRefresh() {
        return !this.assignment.id
    }
    async provideCodeLenses(document: TextDocument): Promise<CodeLens[]> {
        const range: Range = new Range(document.lineCount - 1, 0, document.lineCount - 1, 0);
        const { id, assignmentId, language, taskId } = this.assignment
        return [this.getCodeLens(range, {
            assignmentId,
            id,
            taskId,
            userId: twsManager.getUser().userId,
            language,
            userAnswerCode: await this.getUserAnswer(document),
            filePath: document.fileName
        })];
    }
    getUserAnswer(document: TextDocument) {
        return fse.readFileSync(document.fileName, 'utf8')
    }

    getCodeLens(range: Range, args: any) {
        if (this.codeLens) {
            this.codeLens.arguments = [args]
        }
        return new CodeLens(range, {
            title: "Submit",
            command: SUBMIT_SINGLE_LANGUAGE_CODING,
            arguments: [args],
        })
    }
}


export const singleLanguageCodeingLensProvider = new SingleLanguageCodeingLensProvider();